﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using CCL.TableStructure;
using System.Data;

namespace Server.Service
{
    /// <summary>
    /// 
    /// </summary>
    public class ShowLog : IService
    {
        /// <summary>
        /// 요청받은 메시지의 본문
        /// </summary>
        CCL.SocketMessage.Request.ShowLog m_body;

        /// <summary>
        /// 새로운 서비스를 생성합니다.
        /// </summary>
        /// <param name="body">요청받은 메시지 본문</param>
        public ShowLog(CCL.SocketMessage.Request.ShowLog body)
        {
            m_body = body;
        }
        
        /// <summary>
        /// 서비스를 시작합니다.
        /// </summary>
        /// <returns>전송할 byte배열</returns>
        public byte[] Execute()
        {
            byte[] returnBytes = null;

            using (SqlConnection sqlCon = new SqlConnection(Properties.Resources.ConnectionString))
            {
                sqlCon.Open();
                try
                {
                    SqlCommand sqlCmd = new SqlCommand(@"
                        SELECT 
                            [ProjectRevision].revisionId AS revId,
                            [ProjectRevision].revision AS rev,
                            [ProjectRevision].dateTime AS revDate,
                            [ProjectRevision].message AS log,
                            [User].userId AS uid, 
                            [User].email AS email, 
                            [User].name AS name, 
                            [User].password AS password, 
                            [User].joinedDateTime AS joinedDate 
                        FROM [ProjectRevision]
                        INNER JOIN [User]
                            ON [ProjectRevision].userId = [User].userId
                        WHERE [ProjectRevision].ProjectId = @pid
                    ", sqlCon);

                    sqlCmd.Parameters.AddWithValue("@pid", m_body.ProjectID);

                    SqlDataAdapter da = new SqlDataAdapter(sqlCmd);
                    DataSet ds = new DataSet();
                    da.Fill(ds);

                    // 성공 메시지 빌드
                    CCL.SocketMessage.Response.ShowLog sl = new CCL.SocketMessage.Response.ShowLog();
                    
                    foreach (DataRow row in ds.Tables[0].Rows)
                    {
                        int revId, rev, uid;
                        string log, email, name, password;
                        DateTime revDate, joinedDate;

                        revId = (int)row["revId"];
                        rev = (int)row["rev"];
                        uid = (int)row["uid"];

                        log = (string)row["log"];
                        email = (string)row["email"];
                        name = (string)row["name"];
                        password = (string)row["password"];

                        revDate = (DateTime)row["revDate"];
                        joinedDate = (DateTime)row["joinedDate"];

                        User userInfo = new User(uid, name, email, password, joinedDate);
                        ProjectRevision prev = new ProjectRevision(revId, rev, userInfo, revDate, log);

                        sl.RevisionDataList.Add(prev);
                    }

                    CCL.SocketMessage.Message msg = CCL.SocketMessage.Message.MessageResponseBuilder(sl);

                    returnBytes = msg.GetByte();
                }
                catch (System.Exception ex)
                {
                    // 실패 메시지 빌드
                    CCL.SocketMessage.Response.ShowLog sl = new CCL.SocketMessage.Response.ShowLog(ex.Message);
                    CCL.SocketMessage.Message msg = CCL.SocketMessage.Message.MessageResponseBuilder(sl);

                    returnBytes = msg.GetByte();
                }
                finally
                {
                    sqlCon.Close();
                }
            }

            return returnBytes;
        }
    }
}
